package com.vcredit.bird.service.bankcard;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.bird.api.IBankCardService;
import com.vcredit.bird.enumtype.BankEnum;
import com.vcredit.bird.enumtype.CreditCardBankEnum;
import com.vcredit.bird.service.base.RMC_BaseRespVo;
import com.vcredit.bird.service.base.RMCloudService;
import com.vcredit.bird.util.BirdConstants;
import com.vcredit.bird.vo.custcard.BankCardBin;
import com.vcredit.bird.vo.custcard.BankVerifyParam;
import com.vcredit.bird.vo.custcard.BankVerifyResponse;
import com.vcredit.bird.vo.custcard.BankVerifyResult;
import com.vcredit.creditcat.enumtype.BankCardTypeEnum;
import com.vcredit.creditcat.exception.CustCardExcetpion;
import com.vcredit.creditcat.util.EnumExtUtil;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;

@Service
public class VcreditBankCardService extends RMCloudService implements IBankCardService {

  /**
   * 验卡接口url
   */
  @Value("${card.verify}")
  private String VERIFY_CARD_URL;

  /**
   * 查询卡bin url
   */
  @Value("${card.queryBin}")
  private String QUERY_CARD_BIN_URL;

  @Override
  @Log
  public BankCardBin queryCardBin(String cardNo) throws CustCardExcetpion {
    try {

      BankCardBin bankCardBin =
          JacksonUtil.Json2Obj(HttpRequest.doJson(QUERY_CARD_BIN_URL, cardNo),
              new TypeReference<BankCardBin>() {});
      if (bankCardBin.getIsSuccess()) {
        // 判断银行卡所属银行是否在支持的银行列表内
        BankEnum bankEnum = null;
        CreditCardBankEnum creditCardBankEnum = null;
        if (bankCardBin.getIsCreditCard().equals(BankCardTypeEnum.DEBIT.getDesc())) {
          // 借记卡
          bankEnum =
              EnumExtUtil.getEnumOnValue(BankEnum.class, bankCardBin.getCardIssuingBankName(),
                  "desc");
        } else {
          // 贷记卡
          creditCardBankEnum =
              CreditCardBankEnum.getBankByName(bankCardBin.getCardIssuingBankName());
        }

        if (bankEnum == null && creditCardBankEnum == null) {
          throw new CustCardExcetpion("不支持的银行卡，请更换一张银行卡");
        } else {
          bankCardBin.setBankEnum(bankEnum);
          bankCardBin.setCreditCardBankEnum(creditCardBankEnum);
          return bankCardBin;
        }
      } else {
        throw new CustCardExcetpion(bankCardBin.getErrorInfo());
      }
    } catch (Exception e) {
      e.printStackTrace();
      throw new CustCardExcetpion(e.getMessage());
    }
  }

  @Override
  @Log
  public Boolean verifyCard(String cardNo, String name, String idNo, String mobile, String bankNo,
      String source) throws CustCardExcetpion {
    BankVerifyParam param = new BankVerifyParam();
    param.setBankCardNo(cardNo);
    param.setRealName(name);
    param.setIdCode(idNo);
    param.setPhoneNum(StringUtils.isEmpty(mobile) ? "13888888888" : mobile);
    param.setAccountBank(bankNo);
    param.setSource(source);

    try {
      RMC_BaseRespVo rmcReponse =
          super.postRMCloudData(VERIFY_CARD_URL, getClass().getSimpleName(),
              JacksonUtil.Obj2Json(param));

      BankVerifyResponse<BankVerifyResult> response =
          JacksonUtil.Json2Obj(rmcReponse.getData(),
              new TypeReference<BankVerifyResponse<BankVerifyResult>>() {});
      if (BirdConstants.RMC_REQUEST_SUCCESS.equals(response.getStatus())) {
        if (BirdConstants.STRING_SUCCESS.equals(response.getResult().getAuResult())) {
          return true;
        } else {
          throw new CustCardExcetpion(response.getResult().getAuResultInfo());
        }
      }
      throw new CustCardExcetpion(response.getErrorContent());
    } catch (CustCardExcetpion e) {
      e.printStackTrace();
      throw new CustCardExcetpion(e.getMessage());
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e.getMessage());
    }

  }
}
